package slxy.jiangtao.dts.core.common.hibernate.qbc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PagerUtil {
    private static final Logger logger = LoggerFactory.getLogger(PagerUtil.class);

    private PagerUtil() {
    }

    public static int getOffset(int rowCounts, int curPageNO, int pageSize) {
        int offset = 0;
        try {
            if (curPageNO > (int) Math.ceil((double) rowCounts / pageSize))
                curPageNO = (int) Math.ceil((double) rowCounts / pageSize);
            // 得到第几页
            if (curPageNO <= 1) curPageNO = 1;
            // 得到offset
            offset = (curPageNO - 1) * pageSize;
        } catch (Exception e) {
            logger.error("getOffset出错@1: " + e.getMessage());
            e.printStackTrace();
        }
        return offset;
    }

    public static int getcurPageNo(int rowCounts, int curPageNO, int pageSize) {
        try {
            // 得到第几页
            if (curPageNO > (int) Math.ceil((double) rowCounts / pageSize))
                curPageNO = (int) Math.ceil((double) rowCounts / pageSize);
            if (curPageNO <= 1) curPageNO = 1;
        } catch (Exception e) {
            logger.error("getOffset出错@2: " + e.getMessage());
            e.printStackTrace();
        }
        return curPageNO;
    }
}
